﻿Public Class frmNewSections 
    Dim Strsql As String
    Dim _Mysql As New ClsSQLhelper
    Dim On_Load As Boolean = True
    Public Primary_Key As Integer
    Public Section_ID As Integer
    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            If Me.Txtname.Text.Trim = "" Then Exit Sub
            If MsgBox("คุณต้องการบันทึกข้อมูลหรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการบันทึก") = MsgBoxResult.No Then Exit Sub
            Dim DS As New DataSet

            Strsql = "Select * from Section where Section_name= '" & Me.Txtname.Text & "'"

            DS = _Mysql.GetMYSQLDataset(Strsql, "DS_Section", "Section")
            If DS.Tables(0).Rows.Count <> 0 Then
                MsgBox("รายการ นี้มีอยู่แล้วไม่สามารถบันทึกซ้ำได้อีก", MsgBoxStyle.Critical)
                Exit Sub
            End If
            Strsql = "Insert into section (section_name,note,active) values ('" & Me.Txtname.Text & "', '" & Me.TxtNote.Text & "'," & IIf(Me.ChkActive.Checked = True, 1, 0) & ")"

            _Mysql.MySQLExecute(Strsql)


            Dim MySectionID As Integer
            Strsql = "select * from  section where  section_name = '" & Me.Txtname.Text & "'"

            DS = _Mysql.GetMYSQLDataset(Strsql, "DS_section", "section")
            If DS.Tables(0).Rows.Count <> 0 Then

                MySectionID = DS.Tables(0).Rows(0).Item("section_id").ToString()
            End If
            Strsql = "Insert into category (Category_name,note,readonly,active) values ('ไม่ระบุ'," & MySectionID & ",1,1)"

            _Mysql.MySQLExecute(Strsql)

            MsgBox("เพิ่มข้อมูลเข้าเรียบร้อยแล้ว", MsgBoxStyle.Information)
            'Else

           
            Me.DialogResult = Windows.Forms.DialogResult.OK


        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnSave_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub frmNewSections_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub
End Class